package jsudoku.graphics;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import jsudoku.Sudoku;

public class JSudoku extends JPanel {

    private class Key extends KeyAdapter {

        private int x;
        private int y;

        public Key(int pX, int pY) {
            this.x = pX;
            this.y = pY;
        }

        @Override
        public void keyPressed(KeyEvent e) {
            char pressed = e.getKeyChar();
            if (pressed == '1' || pressed == '2' || pressed == '3' || pressed == '4' || pressed == '5' || pressed == '6' || pressed == '7' || pressed == '8' || pressed == '9') {
                sudoku.addNumber(this.x, this.y, Integer.parseInt(pressed + ""));
                //BUG5
                
                ground[this.x][this.y].setBorder(new LineBorder(Color.BLACK));
                ground[this.x][this.y].removeKeyListener(this);
                repaintSudoku();
            }else if(e.getKeyCode()==KeyEvent.VK_DELETE){
                sudoku.emptyNumber(this.x, this.y);
                ground[this.x][this.y].setBorder(new LineBorder(Color.BLACK));
                ground[this.x][this.y].removeKeyListener(this);
                repaintSudoku();
            }
        }
    }

    private class Mouse extends MouseAdapter {

        private int x;
        private int y;

        public Mouse(int pX, int pY) {
            this.x = pX;
            this.y = pY;
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    ground[i][j].setBorder(new LineBorder(Color.BLACK));
                    KeyListener[] remove = ground[i][j].getKeyListeners();
                    if (remove.length != 0) {
                        ground[i][j].removeKeyListener(remove[0]);
                    }
                }
            }
            ground[this.x][this.y].requestFocus();
            ground[this.x][this.y].setBorder(new LineBorder(Color.RED));
            ground[this.x][this.y].addKeyListener(new Key(this.x, this.y));
        }
    }
    private Sudoku sudoku;
    private JLabel[][] ground;
   

    public JSudoku() {
        this(new Sudoku());
    }

    public Sudoku getSudoku() {
        return this.sudoku;
    }

    public JSudoku(Sudoku pSudoku) {
        this.sudoku = pSudoku;
        this.setSize(300, 300);
        this.setBackground(Color.WHITE);
        this.setLayout(new GridLayout(9, 9));
        this.initLabel();
    }

    public void setSudoku(Sudoku pSudoku) {
        this.sudoku = pSudoku;
        this.repaintSudoku();
        this.initListeners();
    }

    public void initListeners() {

        int[][] sud = this.sudoku.getSudoku();
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                MouseListener[] listener=this.ground[i][j].getMouseListeners();
                for(int x=0; x<listener.length; x++){
                    this.ground[i][j].removeMouseListener(listener[x]);
                }
                if (sud[i][j] == 0) {
                    this.ground[i][j].addMouseListener(new Mouse(i, j));
                    this.ground[i][j].setFocusable(true);
                } else {
                    this.ground[i][j].setFocusable(false);
                }
            }
        }
    }

    public void repaintSudoku() {
        int[][] sud = this.sudoku.getSudoku();
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (sud[i][j] != 0) {
                    this.ground[i][j].setText(sud[i][j] + "");
                } else {
                    this.ground[i][j].setText(" ");
                }
            }
        }
    }

    private void initLabel() {
        this.ground = new JLabel[9][9];
        int[][] sud = this.sudoku.getSudoku();
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                String text = " ";
                if (sud[i][j] != 0) {
                    text = sud[i][j] + "";
                }
                this.ground[i][j] = new JLabel(text);
                this.ground[i][j].setBorder(new LineBorder(Color.BLACK));
                this.ground[i][j].setHorizontalAlignment(JLabel.CENTER);
                this.add(this.ground[i][j]);
            }
        }
        this.initListeners();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(100, 0, 100, 100);
        g.fillRect(0, 100, 100, 100);
        g.fillRect(199, 99, 100, 100);
        g.fillRect(99, 199, 100, 100);
    }
}
